package com.hotchpotch.util;

import android.content.Context;
import android.hardware.Camera;
import android.view.View;
import android.widget.TextView;

import java.io.InputStream;

/**
 * @author yangyu
 *         功能描述：常量工具类
 */
public class MyUtil {

    /**
     * @author miao
     * @createTime 2017年2月10日
     * @lastModify 2017年2月10日
     * @param
     * @return
     */
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            canUse = false;
        }
        if (canUse) {
            if (mCamera != null)
                mCamera.release();
            mCamera = null;
        }
        return canUse;
    }

    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
    }

    /**
     * 得到设备屏幕的宽度
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 得到设备屏幕的高度
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 得到设备的密度
     */
    public static float getScreenDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    /**
     * 把密度转换为像素
     */
    public static int dip2px(Context context, float px) {
        final float scale = getScreenDensity(context);
        return (int) (px * scale + 0.5);
    }

    /**
     * 读取assets文件夹的文件
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String readAssert(Context context, String fileName) {
        String resultString = "";
        try {
            InputStream inputStream = context.getResources().getAssets().open(fileName);
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            resultString = new String(buffer, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * 倒计时时间戳转化为时分秒格式
     *
     * @param time
     * @return
     */
    public static String countDownTimerHour(long time) {
        //得到小时
        long hour = time / 1000 / 60 / 60;
        //减去小时
        long time1 = time - ((int) hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time1 / 1000 / 60;
        //减去分
        long time2 = time1 - ((int) minute * 60 * 1000);
        //得到秒
        long secs = time2 / 1000;
        return (int) hour + "小时" + (int) minute + "分" + secs + "秒";
    }

    /**
     * 倒计时时间戳转化为时分秒格式
     *
     * @param time
     * @return
     */
    public static String countDownTimerDay(long time) {
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        //减去天得到小时
        long time1 = time - ((int) day * 24 * 60 * 60 * 1000);
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        //减去小时
        long time2 = time1 - ((int) hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time2 / 1000 / 60;
        //减去分
        long time3 = time2 - ((int) minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        return (int) day + "天" + (int) hour + "小时" + (int) minute + "分" + secs + "秒";
    }

    /**
     * 倒计时得到单独的时分秒
     *
     * @param time
     * @param textViews
     */
    public static void countDownTimerTextview(long time, TextView[] textViews) {
        //得到天
        long day = time / 1000 / 60 / 60 / 24;
        //减去天得到小时的毫秒
        long time1 = time - ((int) day * 24 * 60 * 60 * 1000);
        //得到小时
        long hour = time1 / 1000 / 60 / 60;
        //减去小时得到分钟数的毫秒
        long time2 = time1 - ((int) hour * 60 * 60 * 1000);
        //得到分钟数
        long minute = time2 / 1000 / 60;
        //减去分得到秒的毫秒
        long time3 = time2 - ((int) minute * 60 * 1000);
        //得到秒
        long secs = time3 / 1000;
        if (textViews.length == 6) {
            String h = (int) hour + "";
            String m = (int) minute + "";
            String s = (int) secs + "";
            if (h.length() == 2) {
                textViews[0].setText(h.charAt(0) + "");
                textViews[1].setText(h.charAt(1) + "");
            } else {
                textViews[0].setText(0 + "");
                textViews[1].setText(h);
            }
            if (m.length() == 2) {
                textViews[2].setText(m.charAt(0) + "");
                textViews[3].setText(m.charAt(1) + "");
            } else {
                textViews[2].setText(0 + "");
                textViews[3].setText(m);
            }
            if (s.length() == 2) {
                textViews[4].setText(s.charAt(0) + "");
                textViews[5].setText(s.charAt(1) + "");
            } else {
                textViews[4].setText(0 + "");
                textViews[5].setText(s);
            }
        }

        if (textViews.length == 8) {

        }
    }
}
